package com.MoveEat;

import com.MoveEat.Utils.Consts;
import com.MoveEat.Utils.FacebookLoginHelper;
import com.MoveEat.activities.LocationActivity;
import com.MoveEat.activities.SearchActivity;

import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TextView;

public class MoveEat extends TabActivity
{
	private LocalActivityManager	mlam;
	private int						mMoveEatUID;
	private ImageButton	mFbLogin;
	private TabHost	mTabHost;

	public void onCreate(Bundle paramBundle)
	{
		super.onCreate(paramBundle);
		setContentView(R.layout.tab_layout);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		mMoveEatUID = prefs.getInt(Consts.Extras.ME_UID, -1);
		mFbLogin = (ImageButton) findViewById(R.id.fb_sign);
		new FacebookLoginHelper(this);
		mFbLogin.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (mMoveEatUID == -1)
				{
					FacebookLoginHelper.getInstance(MoveEat.this).facebookSignIn(MoveEat.this);
					((ImageButton) v).setBackgroundResource(R.drawable.logout_button);
					mMoveEatUID = FacebookLoginHelper.getInstance(MoveEat.this).getMoveEatUID();
				}
				else
				{
					FacebookLoginHelper.getInstance(MoveEat.this).setActivity(MoveEat.this);
					FacebookLoginHelper.getInstance(MoveEat.this).facebookLogout();
					((ImageButton) v).setBackgroundResource(R.drawable.login_button);
					mMoveEatUID = -1;
				}
			}
		});
		if (mMoveEatUID != -1)
		{
			mFbLogin.setBackgroundResource(R.drawable.logout_button);
		}
		TabHost.TabSpec spec;
		Intent tabIntent;
		mTabHost = getTabHost();
		Resources res = getResources();

		LocalActivityManager localLocalActivityManager1 = new LocalActivityManager(this, true);
		this.mlam = localLocalActivityManager1;
		this.mlam.dispatchCreate(paramBundle);
		LocalActivityManager localLocalActivityManager2 = this.mlam;
		mTabHost.setup(localLocalActivityManager2);

		// Location tab:
		tabIntent = new Intent().setClass(this, LocationActivity.class);
		spec = mTabHost.newTabSpec("location").setIndicator(prepareTabView(this, res.getString(R.string.Location), res.getDrawable(R.drawable.ic_tab_location)))
				.setContent(tabIntent);
		mTabHost.addTab(spec);

		// Search tab:
		tabIntent = new Intent().setClass(this, SearchActivity.class);
		spec = mTabHost.newTabSpec("search").setIndicator(prepareTabView(this, res.getString(R.string.Search), res.getDrawable(R.drawable.ic_tab_search)))
				.setContent(tabIntent);
		mTabHost.addTab(spec);

		mTabHost.setCurrentTab(0);

	}

	public static View prepareTabView(Context context, String text, Drawable background)
	{
		View view = LayoutInflater.from(context).inflate(R.layout.native_tabs, null);
		TextView tv = (TextView) view.findViewById(R.id.fakeNativeTabTextView);
		tv.setText(text);
		view.setBackgroundDrawable(background);
		return view;
	}

	@Override
	public void onPause()
	{
		super.onPause();
		this.mlam.dispatchResume();
	}

	@Override
	public void onResume()
	{
		super.onResume();
		FacebookLoginHelper.getInstance(MoveEat.this).setActivity(getCurrentActivity());
		if (FacebookLoginHelper.getInstance(getCurrentActivity()).getMoveEatUID() > 0)
		{
			mFbLogin.setBackgroundResource(R.drawable.logout_button);
		}
		mTabHost.setCurrentTab(1);
		mTabHost.setCurrentTab(0);

		this.mlam.dispatchResume();
	}

}
